home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / BDK / apis / sun / beans / editors / FontEditor.java < prev    next >
Encoding:
Java Source  |  1997-03-17  |  4.7 KB  |  192 lines

  1. /*
  2.  * @(#)FontEditor.java    1.22 97/01/10  
  3.  * 
  4.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion bdk_beta
  20.  * 
  21.  */
  22.  
  23. package sun.beans.editors;
  24.  
  25. import java.awt.*;
  26. import java.beans.*;
  27.  
  28. public class FontEditor extends Panel implements java.beans.PropertyEditor {
  29.  
  30.     public FontEditor() {
  31.     setLayout(null);
  32.  
  33.     toolkit = Toolkit.getDefaultToolkit();
  34.     fonts = toolkit.getFontList();
  35.  
  36.     familyChoser = new Choice();
  37.     for (int i = 0; i < fonts.length; i++) {
  38.         familyChoser.addItem(fonts[i]);
  39.     }
  40.     add(familyChoser);
  41.     familyChoser.reshape(20, 5, 100, 30);
  42.  
  43.     styleChoser = new Choice();
  44.     for (int i = 0; i < styleNames.length; i++) {
  45.         styleChoser.addItem(styleNames[i]);
  46.     }
  47.     add(styleChoser);
  48.     styleChoser.reshape(145, 5, 70, 30);
  49.  
  50.     sizeChoser = new Choice();
  51.     for (int i = 0; i < pointSizes.length; i++) {
  52.         sizeChoser.addItem("" + pointSizes[i]);
  53.     }
  54.     add(sizeChoser);
  55.     sizeChoser.reshape(220, 5, 70, 30);
  56.  
  57.     resize(300,40);
  58.     }
  59.  
  60.  
  61.     public Dimension preferredSize() {
  62.     return new Dimension(300, 40);
  63.     }
  64.  
  65.     public void setValue(Object o) {
  66.     font = (Font) o;
  67.     changeFont(font);
  68.         // Update the current GUI choices.
  69.     for (int i = 0; i < fonts.length; i++) {
  70.         if (fonts[i].equals(font.getFamily())) {
  71.         familyChoser.select(i);
  72.             break;
  73.         }
  74.     }
  75.     for (int i = 0; i < styleNames.length; i++) {
  76.         if (font.getStyle() == styles[i]) {
  77.         styleChoser.select(i);
  78.             break;
  79.         }
  80.     }
  81.     for (int i = 0; i < pointSizes.length; i++) {
  82.         if (font.getSize() <= pointSizes[i]) {
  83.         sizeChoser.select(i);
  84.             break;
  85.         }
  86.     }
  87.     }
  88.  
  89.     private void changeFont(Font f) {
  90.     font = f;
  91.     if (sample != null) {
  92.         remove(sample);
  93.     }
  94.     sample = new Label(sampleText);
  95.     sample.setFont(font);
  96.     add(sample);
  97.     Component p = getParent();
  98.     if (p != null) {
  99.         p.invalidate();
  100.         p.layout();
  101.     }
  102.     invalidate();
  103.     layout();
  104.     repaint();
  105.     support.firePropertyChange("", null, null);
  106.     }
  107.  
  108.     public Object getValue() {
  109.     return (font);
  110.     }
  111.  
  112.     public String getJavaInitializationString() {
  113.     return "new java.awt.Font(\"" + font.getFamily() + "\", " +
  114.                  font.getStyle() + ", " + font.getSize() + ")";
  115.     }
  116.  
  117.     public boolean action(Event e, Object arg) {
  118.     String family = familyChoser.getSelectedItem();
  119.     int style = styles[styleChoser.getSelectedIndex()];
  120.     int size = pointSizes[sizeChoser.getSelectedIndex()];
  121.     try {
  122.         Font f = new Font(family, style, size);
  123.         changeFont(f);
  124.     } catch (Exception ex) {
  125.         System.err.println("Couldn't create font " + family + "-" +
  126.             styleNames[style] + "-" + size);
  127.     }
  128.     return (false);
  129.     }
  130.  
  131.  
  132.     public boolean isPaintable() {
  133.     return true;
  134.     }
  135.  
  136.     public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
  137.     // Silent noop.
  138.     Font oldFont = gfx.getFont();
  139.     gfx.setFont(font);
  140.     FontMetrics fm = gfx.getFontMetrics();
  141.     int vpad = (box.height - fm.getAscent())/2;
  142.     gfx.drawString(sampleText, 0, box.height-vpad);
  143.     gfx.setFont(oldFont);
  144.     }
  145.  
  146.     public String getAsText() {
  147.     return null;
  148.     }
  149.  
  150.     public void setAsText(String text) throws IllegalArgumentException {
  151.     throw new IllegalArgumentException(text);
  152.     }
  153.  
  154.     public String[] getTags() {
  155.     return null;
  156.     }
  157.  
  158.     public java.awt.Component getCustomEditor() {
  159.     return this;
  160.     }
  161.  
  162.     public boolean supportsCustomEditor() {
  163.     return true;
  164.     }
  165.  
  166.     public void addPropertyChangeListener(PropertyChangeListener l) {
  167.     support.addPropertyChangeListener(l);
  168.     }
  169.  
  170.     public void removePropertyChangeListener(PropertyChangeListener l) {
  171.     support.removePropertyChangeListener(l);
  172.     }
  173.  
  174.     private Font font;
  175.     private Toolkit toolkit;
  176.     private String sampleText = "Abcde...";
  177.  
  178.     private Label sample;
  179.     private Choice familyChoser;
  180.     private Choice styleChoser;
  181.     private Choice sizeChoser;
  182.  
  183.     private String fonts[];
  184.     private String[] styleNames = { "plain", "bold", "italic" };
  185.     private int[] styles = { Font.PLAIN, Font.BOLD, Font.ITALIC };
  186.     private int[] pointSizes = { 3, 5, 8, 10, 12, 14, 18, 24, 36, 48 };
  187.  
  188.     private PropertyChangeSupport support = new PropertyChangeSupport(this);
  189.  
  190. }
  191.  
  192.